package com.zaimokuza.cn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.blog.Classification;
import com.zaimokuza.cn.entity.enums.CustomizeExceptionEnum;
import com.zaimokuza.cn.mapper.ClassificationMapper;
import com.zaimokuza.cn.service.ClassificationService;
import com.zaimokuza.cn.util.AssertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author zaimokuza
 */
@Service
public class ClassificationServiceImpl implements ClassificationService {

    @Resource
    private ClassificationMapper classificationMapper;

    @Override
    public void addClassification(Classification classification) {
        // 校验名称不能重复
        Integer nameIsRepeat = classificationMapper.queryClassificationNameIsRepeat(classification.getId(), classification.getUserId(), classification.getName());
        AssertUtils.isNull(nameIsRepeat, CustomizeExceptionEnum.CLASSIFICATION_NAME_IS_REPEAT);
        // 添加记录
        classificationMapper.insertClassificationRecord(classification);
    }

    @Override
    public void updateClassification(Classification classification) {
        // 检验是否有权限操作分类
        Integer classificationIsExist = classificationMapper.queryClassificationIsExist(classification.getId(), classification.getUserId());
        AssertUtils.notNull(classificationIsExist, CustomizeExceptionEnum.CLASSIFICATION_NOT_FOUND);
        // 校验名称不能重复
        Integer nameIsRepeat = classificationMapper.queryClassificationNameIsRepeat(classification.getId(), classification.getUserId(), classification.getName());
        AssertUtils.isNull(nameIsRepeat, CustomizeExceptionEnum.CLASSIFICATION_NAME_IS_REPEAT);
        // 更新记录
        classificationMapper.updateClassificationRecord(classification);
    }

    @Override
    public void deleteClassificationForUser(String id, String userId) {
        // 检验是否有权限操作分类
        Integer classificationIsExist = classificationMapper.queryClassificationIsExist(id, userId);
        AssertUtils.notNull(classificationIsExist, CustomizeExceptionEnum.CLASSIFICATION_NOT_FOUND);
        // 删除分类
        deleteClassification(id);
    }

    @Override
    public void deleteClassification(String id) {
        // 校验分类下是否存在博客
        Integer classificationIsUse = classificationMapper.queryClassificationIsUse(id);
        AssertUtils.isNull(classificationIsUse, CustomizeExceptionEnum.CLASSIFICATION_IS_USE);
        // 删除分类信息
        classificationMapper.deleteClassificationRecord(id);
    }

    @Override
    public ArrayList<Classification> getClassificationListByUserId(String userId) {
        return classificationMapper.queryClassificationListByUserId(userId);
    }

    @Override
    public ArrayList<Classification> getClassificationListByUserDomain(String userDomain) {
        return classificationMapper.queryClassificationListByUserDomain(userDomain);
    }

    @Override
    public PageResult<Classification> getClassificationPage(String name, String userName, Integer page, Integer size) {
        // 分页
        PageHelper.startPage(page, size);
        // 查询
        Page<Classification> classificationPage = classificationMapper.queryClassificationPage(name, userName);
        // 封装结果并返回
        return PageResult.from(classificationPage);
    }

}
